package cn.yunhe.controller;

import cn.yunhe.entity.PayBack;

import cn.yunhe.exportExcel.ExcelBeanUtil;
import cn.yunhe.exportExcel.ExcelUtil;
import cn.yunhe.exportExcel.WebUtil;
import cn.yunhe.service.impl.PayBackServiceImpl;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author MSJ
 * @date 2018/9/19 19:56
 * poi 导出
 */
@Controller
public class PoiController {
    private static final Logger log=LoggerFactory.getLogger(PoiController.class);

    @Autowired
    private PayBackServiceImpl payBackService;

    @Value("payBackRecord")
    private String sheetProductName;

    @Value("payBackRecord.xls")
    private String excelProductName;



    /**
     * 下载excel
     * @param response
     * @return
     */
    @RequestMapping(value="/excel/export",method=RequestMethod.GET)
    public @ResponseBody
    String exportExcel(HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        try {
            List<PayBack> payBacks=payBackService.queryAllPayList();
            String[] headers = new String[]{ "赔案编号", " 案件编号"," 保单编号",  "收款人","身份证号", "联系方式","车牌号", "信用卡",  "赔付金额", "赔付日期", "业务员"};
            List<Map<Integer, Object>> dataList=ExcelBeanUtil.manageProductList(payBacks);
            log.info("excel下载填充数据： {} ",dataList);

            Workbook wb=new HSSFWorkbook();
            ExcelUtil.fillExcelSheetData(dataList, wb, headers, sheetProductName);
            WebUtil.downloadExcel(response, wb, excelProductName);
            return excelProductName;
        } catch (Exception e) {
            log.error("下载excel 发生异常：",e.fillInStackTrace());
        }
        return null;
    }
}
